//#define __FORCE_TRACE__
//#define __FORCE_DEBUG__
//#define __FORCE_INFO__
#define THIS_FILE "tools\fn_buildingPosArray.sqf"
#include <tmf_constants.h>
#include <tmf_macros.h>

TRACE_1("Start",_this);
TMFPROFILERSTART;

private ["_house","_array","_pos_types","_current_index","_current_pos","_index","_result","_executed","_sub_result","_fnc_convertType"];
DEFAULT_PARAM(_house,0,objNull);
DEFAULT_PARAM(_pos_types,1,"");

_result = [];
_executed = false;
_array = [];
if (!isnull _house) then {
	if (_house isKindOf "Building") then {
		_fnc_convertType = {
			if (IS_STRING(_this)) then {
				switch (toUpper(_this)) do {
					case "INTERN": {TMF_BUILDING_POS_COMBAT};
					case "INSIDE": {TMF_BUILDING_POS_COMBAT};
					case "COMBAT": {TMF_BUILDING_POS_COMBAT};
					case "DOOR": {TMF_BUILDING_POS_DOOR};
					case "HIDE": {TMF_BUILDING_POS_HIDE};
					case "HIDING": {TMF_BUILDING_POS_HIDE};
					case "SNIPER": {TMF_BUILDING_POS_SNIPER};
					case "SNIPERS": {TMF_BUILDING_POS_SNIPER};
					case "VEH": {TMF_BUILDING_POS_VEHICLE};
					case "VEHICLE": {TMF_BUILDING_POS_VEHICLE};
					case "CAR": {TMF_BUILDING_POS_VEHICLE};
					case "GARAGE": {TMF_BUILDING_POS_VEHICLE};
					default {TMF_BUILDING_POS_COMBAT};
				};
			} else {
			  _this;
			};
		};
		if (IS_STRING(_pos_types)) then {
			_array set [count _array, _pos_types call _fnc_convertType];
		} else {
			if (IS_ARRAY(_pos_types)) then {
				for "_i" from 0 to (count  _pos_types) - 1 do {
					_array set [count _array, ((_pos_types select _i) call _fnc_convertType)];
				};
			} else {
				_array set [count _array, _pos_types];
			};
		};
		DEBUG_1("Searching",ARR_2((typeOf _house),_array));
		if (!isNil "TMF_BUILDING_POS_INDEXER") then {
			// Batiment référencé
			_index = TMF_BUILDING_POS_INDEXER find (typeOf _house);
			if (_index >= 0) then {
				private ["_type","_bld_array","_type_array","_house_position","_house_dir","_pos_details"];
				_bld_array = TMF_BUILDING_POS_DETAIL select _index;
				for "_i" from 0 to (count _array - 1) do {
					_type = _array select _i;
					if (count _bld_array > _type) then {
						_type_array = _bld_array select _type;
						DEBUG_1("Found positions",ARR_3(typeOf _house, _type, _type_array));
						if (count _type_array > 0) then {
							_house_position = getPosATL _house;
							_house_dir = getDir _house;
							for "_j" from 0 to (count _type_array - 1) do {
								_pos_details = _type_array select _j;
								_result set [count _result, ([_house_position,_house_dir,_pos_details] call FFUNC(BuildingPosCalculator))];
							};
						};
					};
				};
				_executed = true;
			};
		};
		// If we didn't find a position using the indexer AND if the position we search is of type Door/Combat, then we use the default ArmA search
		if (!_executed && {count _result <= 0} && {({ _x <= TMF_BUILDING_POS_DOOR} count _array) > 0}) then {
			DEBUG_1("Default code",(typeOf _house));
			// Cas d'un bâtiment inconnu ou non indexé
			_current_index = 0;
			_current_pos = [0,0,0];
			_sub_result = [];
			for "_current_index" from 0 to 49 do
			{
				_current_pos = _house buildingPos _current_index;
				// If the returned position is 0,0,0 it means there are no more positions
				if (abs(_current_pos vectorDistance [0,0,0]) <= 0.01) exitWith {};
				_index = _sub_result find _current_pos;
				if (_index < 0) then {
					_sub_result set [count _sub_result, _current_pos];
				} else {
					// diag_log Format["(%1)[%2]: %3: %4 (%5)",diag_tickTime,"building_position.sqf","Position already existsd", _current_pos,_index];
				};
				_current_index = _current_index + 1;
			};
			DEBUG_1("Found positions",_sub_result);
			// If the position_type is a hide position, we remove the "Exits"
			If (({ _x == TMF_BUILDING_POS_HIDE } count _array) > 0) then {
				_current_index = 0;
				_current_pos = [0,0,0];
				for "_current_index" from 0 to 49 do
				{
					_current_pos = _house buildingExit _current_index;
					// If the returned position is 0,0,0 it means there are no more positions
					if (abs(_current_pos vectorDistance [0,0,0]) <= 0.01) exitWith {};
					_sub_result = _sub_result - _current_pos;
					_current_index = _current_index + 1;
				};
			};
			DEBUG_1("Found positions after doors",_sub_result);
			for "_i" from 0 to (count _sub_result - 1) do {
				_result set [count _result, [_sub_result select _i, random(360), (if(random(10)>5) then {"UP"} else {"MIDDLE"})]];
			};
		};
	} else {
		ERROR_2("Object is not a house",typeof _house,_house);
	};
} else {
	ERROR("House not set");
};

TRACE_1("End",_result);
TMFPROFILERSTOP;
_result;
